﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Threading.Tasks;
using EFEntity;
using Iservices;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Model;
using Newtonsoft.Json;

namespace Hr.Controllers
{
    //interview_status:-1:注销 0:默认 1:登记 2:申请录用 3:审批 4:成功
    public class employController : Controller
    {
        private readonly ILogger<clientController> _logger;
        private readonly MyDbContext myDbContext;
        private readonly Iengage_resumeServices erServices;
        private readonly Iengage_interviewServices eiServices;

        public employController(MyDbContext myDbContext, ILogger<clientController> logger, Iengage_resumeServices erServices, Iengage_interviewServices eiServices)
        {
            this.myDbContext = myDbContext;
            _logger = logger;
            this.erServices = erServices;
            this.eiServices = eiServices;
        }

        [HttpPost]
        public async Task<IActionResult> register_list(int currentPage, string where)
        {
            Dictionary<string, object> list = await erServices.FenYe_resume_Async(currentPage, where);
            return Content(JsonConvert.SerializeObject(list));
        }

        [HttpGet]
        public IActionResult register_list()
        {
            string where = "1=1 and interview_status = '2'";
            ViewData["where"] = where;
            return View();
        }

        [HttpGet]
        public IActionResult register(int id)
        {
            ViewData["id"] = id;
            return View();
        }

        [HttpPost]
        public async Task<ActionResult> register1(int id)
        {
            List<engage_resume> er_list = await erServices.getOne_resume_Async(id);
            int eid = er_list[0].res_id;
            List<engage_interview> ei_list = await eiServices.getOne_interview_AsyncE(eid);
            //int eid = ei_list[0].resume_id;
            //List<engage_resume> er_list = await erServices.getOne_resume_Async(eid);

            //var fd = await myDbContext.engage_interview.FindAsync(id);
            //var er = await myDbContext.engage_resume.FindAsync(id);
            Dictionary<string, object> di = new Dictionary<string, object>();
            di["engage_resume"] = er_list;
            di["engage_interview"] = ei_list;
            return Content(JsonConvert.SerializeObject(di));
        }

        [HttpPut]
        public async Task<IActionResult> register([FromForm] engage_resume test)
        {
            int result;
            result = await erServices.update_resume_Async(test);
            if (result > 0)
            {
                int s = await eiServices.update_ms_interview_status_Async(test.res_id,test.interview_status);
            }
            return Content(JsonConvert.SerializeObject(result));
        }

        [HttpPost]
        public async Task<IActionResult> check_list(int currentPage, string where)
        {
            Dictionary<string, object> list = await erServices.FenYe_resume_Async(currentPage, where);
            return Content(JsonConvert.SerializeObject(list));
        }

        [HttpGet]
        public IActionResult check_list()
        {
            string where = "1=1 and interview_status = '3'";
            ViewData["where"] = where;
            return View();
        }


        [HttpGet]
        public IActionResult check(int id)
        {
            ViewData["id"] = id;
            return View();
        }

        [HttpPost]
        public async Task<ActionResult> check1(int id)
        {
            List<engage_resume> er_list = await erServices.getOne_resume_Async(id);
            int eid = er_list[0].res_id;
            List<engage_interview> ei_list = await eiServices.getOne_interview_AsyncE(eid);
            //int eid = ei_list[0].resume_id;
            //List<engage_resume> er_list = await erServices.getOne_resume_Async(eid);

            //var fd = await myDbContext.engage_interview.FindAsync(id);
            //var er = await myDbContext.engage_resume.FindAsync(id);
            Dictionary<string, object> di = new Dictionary<string, object>();
            di["engage_resume"] = er_list;
            di["engage_interview"] = ei_list;
            return Content(JsonConvert.SerializeObject(di));
        }

        [HttpPut]
        public async Task<IActionResult> check([FromForm] engage_resume test)
        {
            int result;
            result = await erServices.update_resume_Async1(test);
            if (result > 0)
            {
                var fd = await myDbContext.engage_resume.FindAsync(test.res_id);
                
                if (test.interview_status == 4)//发送短信
                {
                    MailMessage mailMsg = new MailMessage();//两个类，别混了，要引入System.Net这个Assembly
                    /*mailMsg.From = new MailAddress("2558802983@qq.com");*///源邮件地址
                    mailMsg.From = new MailAddress("1126928570@qq.com");//源邮件地址 
                    mailMsg.To.Add(new MailAddress(fd.human_email, fd.human_name));//目的邮件地址。可以有多个收件人
                    mailMsg.Subject = "录用通知";//发送邮件的标题 
                    mailMsg.Body = "恭喜你被录用";//发送邮件的内容 
                    SmtpClient client = new SmtpClient("smtp.qq.com");//smtp.163.com，smtp.qq.com
                    client.Credentials = new NetworkCredential("1126928570@qq.com", "ukepokufthinjjfc");
                    //client.Credentials = new NetworkCredential("2558802983@qq.com", "wjy20021119");
                    client.Send(mailMsg);
                }
                List<engage_interview> ei_list = await eiServices.getOne_interview_AsyncE(test.res_id);
                int eid = ei_list[0].resume_id;
                int s = await eiServices.update_ms_interview_status_Async(eid, test.interview_status);
            }
            return Content(JsonConvert.SerializeObject(result));
        }

        [HttpPost]
        public async Task<IActionResult> list(int currentPage, string where)
        {
            Dictionary<string, object> list = await erServices.FenYe_resume_Async(currentPage, where);
            return Content(JsonConvert.SerializeObject(list));
        }

        [HttpGet]
        public IActionResult list()
        {
            string where = "1=1 and interview_status = '4'";
            ViewData["where"] = where;
            return View();
        }

        [HttpGet]
        public IActionResult details(int id)
        {
            ViewData["id"] = id;
            return View();
        }

        [HttpPost]
        public async Task<ActionResult> details1(int id)
        {
            List<engage_resume> er_list = await erServices.getOne_resume_Async(id);
            int eid = er_list[0].res_id;
            List<engage_interview> ei_list = await eiServices.getOne_interview_AsyncE(eid);
            //int eid = ei_list[0].resume_id;
            //List<engage_resume> er_list = await erServices.getOne_resume_Async(eid);

            //var fd = await myDbContext.engage_interview.FindAsync(id);
            //var er = await myDbContext.engage_resume.FindAsync(id);
            Dictionary<string, object> di = new Dictionary<string, object>();
            di["engage_resume"] = er_list;
            di["engage_interview"] = ei_list;
            return Content(JsonConvert.SerializeObject(di));
        }
    }
}
